﻿using System;
using Spring.Validation;
using Spring.Expressions;

namespace SpringSeries.Core.ValidationObjectModel {
    class Program {
        static void Main(string[] args) {
            RequiredValidator _reqVal1 = new RequiredValidator();
            _reqVal1.Test = Expression.Parse("FirstName");

            ConditionValidator _conVal = new ConditionValidator();
            _conVal.Test = Expression.Parse("DateOfBirth != DateTime.MinValue");

            RegularExpressionValidator _regexVal = new RegularExpressionValidator();
            _regexVal.Test = Expression.Parse("FirstName");
            _regexVal.Expression = "[A-Za-z]*";

            RequiredValidator _reqVal2 = new RequiredValidator();
            _reqVal2.Test = Expression.Parse("AddressInfo.City");
            _reqVal2.When = Expression.Parse("AddressInfo.Address!=String.Empty");

            RequiredValidator _reqVal3 = new RequiredValidator();
            _reqVal3.Test = Expression.Parse("AddressInfo.PostalCode");
            _reqVal3.When = Expression.Parse("AddressInfo.Address!=String.Empty");

            ValidatorGroup _personVal = new ValidatorGroup();
            _personVal.Validators.Add(_reqVal1);
            _personVal.Validators.Add(_reqVal2);
            _personVal.Validators.Add(_reqVal3);
            _personVal.Validators.Add(_conVal);
            _personVal.Validators.Add(_regexVal);

            Person _p01 = new Person(1);
            _p01.FirstName = "Matteo";
            _p01.LastName = "Baglini";
            _p01.DateOfBirth = new DateTime(1982, 11, 8);

            Person _p02 = new Person(2);
            _p02.LastName = "Baglini";
            _p02.DateOfBirth = new DateTime(1982, 11, 8);

            Person _p03 = new Person(3);
            _p03.FirstName = "Matteo";
            _p03.LastName = "Baglini";

            Person _p04 = new Person(4);
            _p04.FirstName = "Matteo";
            _p04.LastName = "Baglini";
            _p04.DateOfBirth = new DateTime(1982, 11, 8);
            _p04.AddressInfo.Address = "Via dei Sassi Sgonfi";

            Person _p05 = new Person(5);
            _p05.FirstName = "Matteo";
            _p05.LastName = "Baglini";
            _p05.DateOfBirth = new DateTime(1982, 11, 8);
            _p05.AddressInfo.Address = "Via dei Sassi Sgonfi";
            _p05.AddressInfo.City = "Livorno";
            _p05.AddressInfo.PostalCode = "57100";

            Person _p06 = new Person(6);
            _p06.FirstName = "1234567890";
            _p06.LastName = "Baglini";
            _p06.DateOfBirth = new DateTime(1982, 11, 8);
            _p06.AddressInfo.Address = "Via dei Sassi Sgonfi";
            _p06.AddressInfo.City = "Livorno";
            _p06.AddressInfo.PostalCode = "57100";

            Console.WriteLine(_p01.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p01, null));
            Console.WriteLine();
            Console.WriteLine(_p02.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p02, null));
            Console.WriteLine();
            Console.WriteLine(_p03.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p03, null));
            Console.WriteLine();
            Console.WriteLine(_p04.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p04, null));
            Console.WriteLine();
            Console.WriteLine(_p05.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p05, null));
            Console.WriteLine();
            Console.WriteLine(_p06.ToString());
            Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p06, null));

            Console.Read();
        }
    }
}
